package cn.wetcland.urban.admin.service.impl;

import cn.wetcland.urban.admin.dto.BrandAddDTO;
import cn.wetcland.urban.admin.dto.BrandUpdateDTO;
import cn.wetcland.urban.admin.dto.SubBrandAddDTO;
import cn.wetcland.urban.admin.mapper.UtCarTemplateMapper;
import cn.wetcland.urban.admin.service.UtCarTemplateService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.CopyObjectUtil;
import cn.wetcland.urban.generator.domain.UtCarTemplate;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Wetcland
* @description 针对表【ut_car_template】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtCarTemplateServiceImpl extends ServiceImpl<UtCarTemplateMapper, UtCarTemplate> implements UtCarTemplateService {

    @Resource
    private UtCarTemplateMapper carTemplateMapper;

    @Override
    public Page<UtCarTemplate> getBrandList(BaseConditionSearchDTO searchDTO) {
        Page<UtCarTemplate> utCarTemplatePage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return carTemplateMapper.selectPage(utCarTemplatePage, new QueryWrapper<UtCarTemplate>().eq("parent_id", 0));
    }

    @Override
    public List<UtCarTemplate> getSerialList(Long brandId) {
        return carTemplateMapper.selectList(new QueryWrapper<UtCarTemplate>().eq("parent_id", brandId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCarTemplate(Long id) {
        carTemplateMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBrand(BrandAddDTO brandAddDTO) {
        UtCarTemplate utCarTemplate = new UtCarTemplate();
        utCarTemplate.setCarBrand(brandAddDTO.getCarBrand());
        utCarTemplate.setCarCapacity(brandAddDTO.getCarCapacity());
        utCarTemplate.setParentId(0L);
        carTemplateMapper.insert(utCarTemplate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubBrand(SubBrandAddDTO subBrandAddDTO) {
        UtCarTemplate utCarTemplate = new UtCarTemplate();
        utCarTemplate.setCarBrand(subBrandAddDTO.getCarBrand());
        utCarTemplate.setCarCapacity(subBrandAddDTO.getCarCapacity());
        utCarTemplate.setParentId(subBrandAddDTO.getParentId());
        carTemplateMapper.insert(utCarTemplate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBrand(BrandUpdateDTO updateDTO) {
        UtCarTemplate utCarTemplate = carTemplateMapper.selectById(updateDTO.getCarTemplateId());
        MyAssert.notNull(utCarTemplate, "该车模版不存在");
        CopyObjectUtil.copyPropertiesIgnoreNull(updateDTO, utCarTemplate);
        carTemplateMapper.updateById(utCarTemplate);
    }
}




